import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export const store = new Vuex.Store({
  strict: true,
  state: sessionStorage.getItem('state') ? JSON.parse(sessionStorage.getItem('state')) : {
    loading: false,
    showBack: true,
    title: '',
    userName: '',
    userType: '',
    user: null,
    number: '',
    showMenu: true,
    token: '',
    access_token: '',
    token_type: '',
    refresh_token: '',
    expires_in: '',
    scope: '',
    menus: []
  },
  getters: {
    getShowMenu: (state) => {
      return state.showMenu
    }
  },
  mutations: {
    updateLoading (state, loading) {
      state.loading = loading
    },
    updateShowBack (state, showBack) {
      state.showBack = showBack
    },
    updateTitle (state, title) {
      state.title = title
    },
    updateNumber (state, number) {
      let num = ''
      if (number <= 0) {
        state.number = num
      } else {
        num = number.toString()
        state.number = num
      }
    },
    updateMenu (state, menus) {
      state.menus = menus
    },
    updateUser (state, user) {
      console.log('user.type' + user.type)
      state.user = user
      if (user != null) {
        if (user.name) {
          state.userName = user.name
          state.userType = user.type
        }
      }
    },
    updateToken (state, token) {
      // const obj = JSON.parse(token)
      state.token = token
      state.access_token = token
      // state.token_type = obj.token_type
      // state.refresh_token = obj.refresh_token
      // state.expires_in = obj.expires_in
      // state.scope = obj.scope
    },
    clearnState (state) {
      state.token = null
      state.access_token = null
      state.token_type = null
      state.refresh_token = null
      state.expires_in = null
      state.scope = null
      state.user = null
    }
  },
  actions: {
  }
})
